package com.bl.user.service.impl;

import com.bl.basic.constant.RegisterConstant;
import com.bl.basic.exception.BusinessException;
import com.bl.basic.util.Md5Utils;
import com.bl.basic.util.StrUtils;
import com.bl.user.domain.Logininfo;
import com.bl.user.domain.User;
import com.bl.user.dto.RegisteredDto;
import com.bl.user.mapper.LogininfoMapper;
import com.bl.user.mapper.UserMapper;
import com.bl.user.service.IUserService;
import com.bl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;


    /**
     *  手机注册实现
     * @param dto
     */
    @Override
    public void registered(RegisteredDto dto) {
        // 校验空值
        String phone = dto.getPhone();
        String phoneRegistered = dto.getPhoneRegistered();
        String password = dto.getPassword();
        String confirmPassword = dto.getConfirmPassword();
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("请输入手机号");
        }
        if (StringUtils.isEmpty(phoneRegistered)){
            throw new BusinessException("请输入手机验证码");
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException("请输入密码");
        }
        if (StringUtils.isEmpty(confirmPassword)){
            throw new BusinessException("请再次输入密码");
        }
        // 判断两次密码是否一致
        if (!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致");
        }
        // 校验手机号是否注册过
        if (userMapper.loadByPhone(phone) != null){
            throw new BusinessException("该手机号码已注册，请重新输入手机号码");
        }
        // 校验手机验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if (obj == null){
            throw new BusinessException("该手机验证码已失效，请重新获取");
        }
        // 校验手机验证码是否正确
        String code = obj.toString().split(":")[0];
        if (!code.equalsIgnoreCase(phoneRegistered)){
            throw new BusinessException("该手机验证码已错误，请重新输入");
        }
        // 保存 logininfo数据和user数据
        Logininfo logininfo = logininfo2Dto(dto);
        User user = logininfo2User(logininfo);

        // 先保存login info
        logininfoMapper.save(logininfo);

        // 再把自增的id赋值uesr的logininfo_id
        user.setLogininfoId(logininfo.getId());
        // 在保存user
        userMapper.save(user);
    }

    /**
     * 创建logininfo对象
     * @param dto
     * @return
     */
    private Logininfo logininfo2Dto(RegisteredDto dto){
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        logininfo.setDisable(true);
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        logininfo.setSalt(salt);
        // 对密码加密加盐
        String dbPassword = Md5Utils.encrypByMd5(dto.getPassword() + salt);
        logininfo.setPassword(dbPassword);
        return logininfo;
    }
    /**
     * 获取user对象
     */
    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        // copy相同的属性
        BeanUtils.copyProperties(logininfo,user);
        return  user;
    }
}
